package com.interviewbackend.service.Impl;

import com.interviewbackend.client.InterviewEvaluationClient;
import com.interviewbackend.entity.InterviewRecord;
import com.interviewbackend.mapper.InterviewRecordMapper; // 引入 Mapper
import com.interviewbackend.service.EvaluationService;
import com.interviewbackend.util.PromptBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EvaluationServiceImpl implements EvaluationService {

    private final InterviewEvaluationClient interviewEvaluationClient;
    private final InterviewRecordMapper interviewRecordMapper;  // 注入 Mapper

    @Autowired
    public EvaluationServiceImpl(InterviewEvaluationClient interviewEvaluationClient,
                                 InterviewRecordMapper interviewRecordMapper) {
        this.interviewEvaluationClient = interviewEvaluationClient;
        this.interviewRecordMapper = interviewRecordMapper;
    }

    @Autowired
    private PromptBuilder promptBuilder;

    @Override
    public String generateOverallEvaluationReport(Long interviewId) {
        List<InterviewRecord> interviewRecords = interviewRecordMapper.findByInterviewId(interviewId);

        List<String> questionsList = interviewRecords.stream().map(InterviewRecord::getQuestionText).toList();
        List<String> answersList = interviewRecords.stream().map(InterviewRecord::getAnswerText).toList();
        List<String> evaluationJsonsList = interviewRecords.stream().map(InterviewRecord::getEvaluationJson).toList();

        // ✨ 构造整体分析 prompt
        String prompt = promptBuilder.buildEvaluationPrompt(questionsList, answersList, evaluationJsonsList);

        // ✨ 调用大模型获取报告
        return interviewEvaluationClient.evaluateOverall(prompt);
    }

    // 判断是否追问
    @Override
    public String evaluateInterview(InterviewRecord interviewRecord) {
        String question = interviewRecord.getQuestionText();
        String answer = interviewRecord.getAnswerText();
        String evaluationJson = interviewRecord.getEvaluationJson();

        // ✨ 构造单题评估 prompt
        String prompt = promptBuilder.buildSingleEvaluationPrompt(question, answer, evaluationJson);

        // ✨ 通过统一的 evaluateOverall 函数发送请求
        return interviewEvaluationClient.evaluateOverall(prompt);
    }

}
